package cn.udrm.water.base.v1.waterdelivery.restful;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.udrm.water.base.v1.commons.models.PageModel;
import cn.udrm.water.base.v1.commons.models.RequestListParam;
import cn.udrm.water.base.v1.commons.models.ResponseParam;
import cn.udrm.water.base.v1.commons.models.ZTreeNodeVo;
import cn.udrm.water.base.v1.waterdelivery.dto.CanalSystemDto;
import cn.udrm.water.base.v1.waterdelivery.service.CanalSystemService;

@Api(tags="渠道实体API", description="渠道CRUD操作")
@RestController
@RequestMapping(value="v1/restful/canalSystem")
public class CanalSystemRestful {
	@Autowired
	private CanalSystemService canalSystemService;
	
	/**
	 * 获取渠道树结构（一次性加载）
	 * @return
	 */
	@ApiOperation("获取渠道树结构（一次性加载）")
	@GetMapping(value="getCanalSystemZtree")
	public ResponseParam<List<ZTreeNodeVo>> getCanalSystemZtree() {
		List<ZTreeNodeVo> list = canalSystemService.getCanalSystemZtree();
		return new ResponseParam<List<ZTreeNodeVo>>("数据获取成功", 200, list);
	}
	
	@ApiOperation("获取指定渠道下的闸门")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="渠道GUID")
	@GetMapping(value="getWaterGates/{id}")
	public ResponseParam<CanalSystemDto> getWaterGates(@PathVariable(value="id") String id) {
		CanalSystemDto withWaterGates = canalSystemService.getWithWaterGates(id);
		return new ResponseParam<CanalSystemDto>("数据获取成功", 200, withWaterGates);
	}
	@ApiOperation("获取指定渠道下的子渠道")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="渠道GUID")
	@GetMapping(value="getChildrens/{id}")
	public ResponseParam<CanalSystemDto> getChildrens(@PathVariable(value="id") String id) {
		CanalSystemDto withWaterGates = canalSystemService.getWithChildrens(id);
		return new ResponseParam<CanalSystemDto>("数据获取成功", 200, withWaterGates);
	}
	@ApiOperation("获取指定渠道下的断面")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="渠道GUID")
	@GetMapping(value="getCanalSystemSections/{id}")
	public ResponseParam<CanalSystemDto> getCanalSystemSections(@PathVariable(value="id") String id) {
		CanalSystemDto withWaterGates = canalSystemService.getWithCanalSystemSections(id);
		return new ResponseParam<CanalSystemDto>("数据获取成功", 200, withWaterGates);
	}
	@ApiOperation("获取指定渠道下的跌水")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="渠道GUID")
	@GetMapping(value="getDrops/{id}")
	public ResponseParam<CanalSystemDto> getDrops(@PathVariable(value="id") String id) {
		CanalSystemDto withWaterGates = canalSystemService.getWithDrops(id);
		return new ResponseParam<CanalSystemDto>("数据获取成功", 200, withWaterGates);
	}
	@ApiOperation("获取指定渠道下的倒虹吸")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="渠道GUID")
	@GetMapping(value="getInvertedSiphons/{id}")
	public ResponseParam<CanalSystemDto> getInvertedSiphons(@PathVariable(value="id") String id) {
		CanalSystemDto withWaterGates = canalSystemService.getWithInvertedSiphons(id);
		return new ResponseParam<CanalSystemDto>("数据获取成功", 200, withWaterGates);
	}
	@ApiOperation("获取指定渠道下的扬水站")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="渠道GUID")
	@GetMapping(value="getPumpingStations/{id}")
	public ResponseParam<CanalSystemDto> getPumpingStations(@PathVariable(value="id") String id) {
		CanalSystemDto withWaterGates = canalSystemService.getWithPumpingStations(id);
		return new ResponseParam<CanalSystemDto>("数据获取成功", 200, withWaterGates);
	}
	@ApiOperation("获取指定渠道下的渡槽")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="渠道GUID")
	@GetMapping(value="getSluices/{id}")
	public ResponseParam<CanalSystemDto> getSluices(@PathVariable(value="id") String id) {
		CanalSystemDto withWaterGates = canalSystemService.getWithSluices(id);
		return new ResponseParam<CanalSystemDto>("数据获取成功", 200, withWaterGates);
	}
	@ApiOperation("获取指定渠道下的陡坡")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="渠道GUID")
	@GetMapping(value="getSteepSlopes/{id}")
	public ResponseParam<CanalSystemDto> getSteepSlopes(@PathVariable(value="id") String id) {
		CanalSystemDto withWaterGates = canalSystemService.getWithSteepSlopes(id);
		return new ResponseParam<CanalSystemDto>("数据获取成功", 200, withWaterGates);
	}
	@ApiOperation("获取指定渠道下的隧洞")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="渠道GUID")
	@GetMapping(value="getTunnels/{id}")
	public ResponseParam<CanalSystemDto> getTunnels(@PathVariable(value="id") String id) {
		CanalSystemDto withWaterGates = canalSystemService.getWithTunnels(id);
		return new ResponseParam<CanalSystemDto>("数据获取成功", 200, withWaterGates);
	}
	
	@ApiOperation("列表数据获取")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType="body", name="requestListParam", required=false, value="列表查询条件", dataType="RequestListParam«CanalSystemDto»")
	})
	@PostMapping(value="list")
	public ResponseParam<PageModel<CanalSystemDto>> list(@RequestBody(required=false) RequestListParam<CanalSystemDto> requestListParam) throws UnsupportedEncodingException {
		if (requestListParam == null) {
			requestListParam = new RequestListParam<>();
		}
		PageModel<CanalSystemDto> pageByConditions = canalSystemService.getPageByConditions(requestListParam.getNumber()
													,requestListParam.getSize()
													,requestListParam.getEntity()
													,requestListParam.getSortField()
													,requestListParam.getSortType());
		return new ResponseParam<PageModel<CanalSystemDto>>("数据获取成功", 200, pageByConditions);
	}
	
	@ApiOperation("单条数据获取")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="渠道GUID")
	@GetMapping(value="get/{id}")
	public ResponseParam<CanalSystemDto> get(@PathVariable(value="id") String id) {
		CanalSystemDto canalSystemDto = canalSystemService.getSelfById(id);
		return new ResponseParam<CanalSystemDto>("数据获取成功", 200, canalSystemDto);
	}
	
	@ApiOperation("渠道数据保存")
	@ApiImplicitParam(name="canalSystemDto", value="需要添加的渠道", required=true, dataType="CanalSystemDto", dataTypeClass=CanalSystemDto.class)
	@PostMapping(value="save")
	public ResponseParam<CanalSystemDto> save(@RequestBody CanalSystemDto canalSystemDto) {
		canalSystemDto = canalSystemService.save(canalSystemDto);
		
		return new ResponseParam<CanalSystemDto>("数据保存成功", 200, canalSystemDto);
	}
	
	@ApiOperation("渠道数据更新")
	@ApiImplicitParam(name="canalSystemDto", value="需要更新的渠道", required=true, dataType="CanalSystemDto", dataTypeClass=CanalSystemDto.class)
	@PostMapping(value="update")
	public ResponseParam<CanalSystemDto> update(@RequestBody CanalSystemDto canalSystemDto) {
		if (canalSystemDto == null || StringUtils.isBlank(canalSystemDto.getId())) {
			return new ResponseParam<CanalSystemDto>("待更新数据没有主键", 500);
		}
		canalSystemDto = canalSystemService.update(canalSystemDto);
		
		return new ResponseParam<CanalSystemDto>("数据更新成功", 200, canalSystemDto);
	}

	@ApiOperation("渠道数据删除（仅删除自身，不能删除标识有实体间关系）")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="渠道GUID")
	@GetMapping(value="deleteForce/{id}")
	public ResponseParam<String> deleteForce(@PathVariable(value="id") String id) {
		try {
			canalSystemService.deleteAllSelf(new String[]{id});
		} catch (Exception e) {
			return new ResponseParam<String>("删除失败", 500);
		}
		return new ResponseParam<String>("删除成功", 200);
	}
}
